/*
'  DotNetNuke -  http://www.dotnetnuke.com
'  Copyright (c) 2002-2011
'  by DotNetNuke Corporation
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Globalization;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Tokens;

namespace DotNetNuke.Modules.Store.Customer
{
  /// <summary>
  /// Summary description for OrderInfo.
  /// </summary>
    public sealed class OrderInfo : IHydratable, IEquatable<OrderInfo>, IPropertyAccess
    {
		#region Constructor

        public OrderInfo()
		{
		}

		#endregion

        #region Private Declarations

        private int _OrderID;
        private int _CustomerID;
        private string _OrderNumber;
        private DateTime _OrderDate;
        private DateTime _ShipDate;
        private int _ShippingAddressID;
        private int _BillingAddressID;
        private decimal _OrderTotal;
        private decimal _TaxTotal;
        private decimal _ShippingCost;
        private decimal _ShippingTax;
        private int _OrderStatusID;
        private string _Status;
        private string _DateFormat;
        private string _CurrencySymbol;
        private string _CommentToCustomer;

        #endregion

        #region Public Properties

        public enum OrderStatusList
        {
            Processing = 1,
            AwaitingPayment = 2,
            AwaitingStock = 3,
            Packing = 4,
            Dispatched = 5,
            Cancelled = 6,
            Paid = 7
        }

        public int OrderID
        {
            get { return _OrderID; }
            set { _OrderID = value; }
        }

        public int CustomerID
        {
            get { return _CustomerID; }
            set { _CustomerID = value; }
        }

        public string OrderNumber
        {
            get { return _OrderNumber; }
            set { _OrderNumber = value; }
        }

        public DateTime OrderDate
        {
            get { return _OrderDate; }
            set { _OrderDate = value; }
        }

        public DateTime ShipDate
        {
            get { return _ShipDate; }
            set { _ShipDate = value; }
        }

        public int ShippingAddressID
        {
            get { return _ShippingAddressID; }
            set { _ShippingAddressID = value; }
        }

        public int BillingAddressID
        {
            get { return _BillingAddressID; }
            set { _BillingAddressID = value; }
        }

        /// <summary>
        /// The subtotal of the cart items in the order.
        /// NOTE: Does NOT include tax and shipping charges.
        /// </summary>
        public decimal OrderTotal
        {
            get { return _OrderTotal; }
            set { _OrderTotal = value; }
        }

        /// <summary>
        /// The grand total summing the OrderTotal, TaxTotal, and ShippingCost
        /// </summary>
        public decimal GrandTotal
        {
            get { return _OrderTotal + _TaxTotal + _ShippingCost; }
        }

        /// <summary>
        /// Total tax that has been calculated for this order.
        /// </summary>
        public decimal TaxTotal
        {
            get { return _TaxTotal; }
            set { _TaxTotal = value; }
        }

        /// <summary>
        /// Shiping costs associated with this order.
        /// </summary>
        public decimal ShippingCost
        {
            get { return _ShippingCost; }
            set { _ShippingCost = value; }
        }

        /// <summary>
        /// Shiping tax that has been calculated for this order.
        /// </summary>
        public decimal ShippingTax
        {
            get { return _ShippingTax; }
            set { _ShippingTax = value; }
        }

        public int OrderStatusID
        {
            get { return _OrderStatusID; }
            set { _OrderStatusID = value; }
        }

        public string Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        public string DateFormat
        {
            get { return _DateFormat; }
            set { _DateFormat = value; }
        }

        public string CurrencySymbol
        {
            get { return _CurrencySymbol; }
            set { _CurrencySymbol = value; }
        }

        public string CommentToCustomer
        {
            get { return _CommentToCustomer; }
            set { _CommentToCustomer = value; }
        }

        #endregion

        #region Object Overrides

        public override int GetHashCode()
        {
            return _OrderID.GetHashCode();
        }

        #endregion

        #region IHydratable Interface

        public void Fill(System.Data.IDataReader dr)
        {
            _OrderID = Convert.ToInt32(dr["OrderID"]);
            _CustomerID = Convert.ToInt32(Null.SetNull(dr["CustomerID"], _CustomerID));
            _OrderNumber = Convert.ToString(Null.SetNull(dr["OrderNumber"], _OrderNumber));
            _OrderDate = Convert.ToDateTime(dr["OrderDate"]);
            _ShipDate = Convert.ToDateTime(dr["ShipDate"]);
            _ShippingAddressID = Convert.ToInt32(dr["ShippingAddressID"]);
            _BillingAddressID = Convert.ToInt32(dr["BillingAddressID"]);
            _OrderTotal = Convert.ToDecimal(Null.SetNull(dr["OrderTotal"], _OrderTotal));
            _TaxTotal = Convert.ToDecimal(Null.SetNull(dr["Tax"], _TaxTotal));
            _ShippingCost = Convert.ToDecimal(Null.SetNull(dr["ShippingCost"], _ShippingCost));
            //_ShippingTax = Convert.ToDecimal(Null.SetNull(dr["ShippingTax"], _ShippingTax));
            //mOrderIsPlaced = Convert.ToBoolean(Null.SetNull(dr["OrderIsPlaced"], mOrderIsPlaced));
            _OrderStatusID = Convert.ToInt32(dr["OrderStatusID"]);
        }

        public int KeyID
        {
            get { return _OrderID; }
            set { _OrderID = value; }
        }

        #endregion

        #region IEquatable<OrderInfo> Members

        public bool Equals(OrderInfo other)
        {
            if (other == null)
                return false;
            return _OrderID.Equals(other.OrderID);
        }

        #endregion

        #region IPropertyAccess Members

        CacheLevel IPropertyAccess.Cacheability
        {
            get { return CacheLevel.fullyCacheable; }
        }

        string IPropertyAccess.GetProperty(string strPropertyName, string strFormat, CultureInfo formatProvider, UserInfo AccessingUser, Scope AccessLevel, ref bool PropertyNotFound)
        {
            string propertyValue = null;
            switch (strPropertyName.ToLower())
            {
                case "orderid":
                    propertyValue = _OrderID.ToString();
                    break;
                //case "ordernumber":
                //    propertyValue = _OrderNumber.ToString();
                //    break;
                case "orderdate":
                    if (string.IsNullOrEmpty(strFormat) == false)
                        propertyValue = _OrderDate.ToString(strFormat);
                    else
                        propertyValue = _OrderDate.ToString(_DateFormat);
                    break;
                case "shipdate":
                    if (string.IsNullOrEmpty(strFormat) == false)
                        propertyValue = _ShipDate.ToString(strFormat);
                    else
                        propertyValue = _ShipDate.ToString(_DateFormat);
                    break;
                case "ordertotal":
                    if (formatProvider != null)
                    {
                        if (string.IsNullOrEmpty(CurrencySymbol) == false)
                            formatProvider.NumberFormat.CurrencySymbol = CurrencySymbol;
                        if (string.IsNullOrEmpty(strFormat) == false)
                            propertyValue = _OrderTotal.ToString(strFormat, formatProvider.NumberFormat);
                        else
                            propertyValue = _OrderTotal.ToString("C", formatProvider.NumberFormat);
                    }
                    else
                        propertyValue = _OrderTotal.ToString("C");
                    break;
                case "grandtotal":
                    if (formatProvider != null)
                    {
                        if (string.IsNullOrEmpty(CurrencySymbol) == false)
                            formatProvider.NumberFormat.CurrencySymbol = CurrencySymbol;
                        if (string.IsNullOrEmpty(strFormat) == false)
                            propertyValue = GrandTotal.ToString(strFormat, formatProvider.NumberFormat);
                        else
                            propertyValue = GrandTotal.ToString("C", formatProvider.NumberFormat);
                    }
                    else
                        propertyValue = GrandTotal.ToString("C");
                    break;
                case "taxtotal":
                    if (formatProvider != null)
                    {
                        if (string.IsNullOrEmpty(CurrencySymbol) == false)
                            formatProvider.NumberFormat.CurrencySymbol = CurrencySymbol;
                        if (string.IsNullOrEmpty(strFormat) == false)
                            propertyValue = _TaxTotal.ToString(strFormat, formatProvider.NumberFormat);
                        else
                            propertyValue = _TaxTotal.ToString("C", formatProvider.NumberFormat);
                    }
                    else
                        propertyValue = _TaxTotal.ToString("C");
                    break;
                case "shippingcost":
                    if (formatProvider != null)
                    {
                        if (string.IsNullOrEmpty(CurrencySymbol) == false)
                            formatProvider.NumberFormat.CurrencySymbol = CurrencySymbol;
                        if (string.IsNullOrEmpty(strFormat) == false)
                            propertyValue = _ShippingCost.ToString(strFormat, formatProvider.NumberFormat);
                        else
                            propertyValue = _ShippingCost.ToString("C", formatProvider.NumberFormat);
                    }
                    else
                        propertyValue = _ShippingCost.ToString("C");
                    break;
                case "shippingtax":
                    if (formatProvider != null)
                    {
                        if (string.IsNullOrEmpty(CurrencySymbol) == false)
                            formatProvider.NumberFormat.CurrencySymbol = CurrencySymbol;
                        if (string.IsNullOrEmpty(strFormat) == false)
                            propertyValue = _ShippingTax.ToString(strFormat, formatProvider.NumberFormat);
                        else
                            propertyValue = _ShippingTax.ToString("C", formatProvider.NumberFormat);
                    }
                    else
                        propertyValue = _ShippingTax.ToString("C");
                    break;
                case "status":
                    propertyValue = _Status;
                    break;
                case "commenttocustomer":
                    propertyValue = _CommentToCustomer;
                    break;
                default:
                    PropertyNotFound = true;
                    break;
            }
            return propertyValue;
        }

        #endregion
    }
}
